Haskell: variant of `show` that doesn't wrap String and Char in quotes

Posted by Joey Adams on Stack Overflow See other posts from Stack Overflow or by Joey Adams
Published on 2010-06-04T02:22:26Z Indexed on 2010/06/08 11:12 UTC
Read the original article Hit count: 272

Filed under:

I'd like a variant of show (let's call it label) that acts just like show, except that it doesn't wrap Strings in " " or Chars in ' '. Examples:

> label 5
"5"
> label "hello"
"hello"
> label 'c'
"c"

I tried implementing this manually, but I ran into some walls. Here is what I tried:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Label where

class (Show a) => Label a where
    label :: a -> String

instance Label [Char] where
    label str = str

instance Label Char where
    label c = [c]

-- Default case
instance Show a => Label a where
    label x = show x

However, because the default case's class overlaps instance Label [Char] and instance Label Char, those types don't work with the label function.

Is there a library function that provides this functionality? If not, is there a workaround to get the above code to work?

© Stack Overflow or respective owner

Related posts about haskell